
package com.probiz.estoresf.solrsearch.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.core.controller.BaseController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.search.SearchCriteriaBuilder;
import com.probiz.estore.solrsearch.model.IndexEntity;
import com.probiz.estore.solrsearch.model.SolrSearchCriteria;
import com.probiz.estore.solrsearch.service.SolrSearchService;

public class SolrSearchController extends BaseController {
	private SolrSearchService solrSearchService;
	
	public void setSolrSearchService(SolrSearchService solrSearchService) {
		this.solrSearchService = solrSearchService;
	}

	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("solrsearch/entityList");
		SolrSearchCriteria solrSearchCriteria = new SolrSearchCriteria();
		solrSearchCriteria.setKeywords(request.getParameter("keywords"));
		SearchCriteriaBuilder scb = new SearchCriteriaBuilder();
		SearchCriteria searchCriteria = scb.buildSearchCriteria(request, 10);
		if(StringUtils.isNotBlank(solrSearchCriteria.getKeywords())){
			try {
				List<IndexEntity>  entityList = solrSearchService.searchByCriteriaForController(solrSearchCriteria, searchCriteria);
				mv.addObject("entityList", entityList);
			} catch (SolrServerException e) {
				e.printStackTrace();
			}
		}
		mv.addObject("solrSearchCriteria", solrSearchCriteria);
		mv.addObject("sc", searchCriteria);
		return mv;

	}

	@Override
	protected void initController() throws Exception {

	}
}
